package com.jinkaimen.kqb.ui.activity;

import android.webkit.WebSettings;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.ui.activity.addmerchant.RegisterAddActivity;
import com.tencent.smtt.utils.TbsLog;

/**
 * Created by lixuebo on 17/7/17.
 */

public class TestWebViewActivity extends BaseActivity {

    private BridgeWebView webView;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("WebViewTest");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_x5webview;
    }

    @Override
    protected void initChildView() {

        webView = (BridgeWebView) findViewById(R.id.x5webview);


        webView.setWebViewClient(new MyWebViewClient(webView));
        //     webView.setDefaultHandler(new myHadlerCallBack());

        webView.loadUrl("http://192.168.1.134:8080/#/");

        //必须和js函数名字一致，注册好具体执行回调函数，类似java实现类。
        webView.registerHandler("showToast", new BridgeHandler() {

            @Override
            public void handler(String data, CallBackFunction function) {
//
//                String str = "好收收 APP测试参数";
//                Toast.makeText(TestWebViewActivity.this, str, Toast.LENGTH_SHORT).show();
//                //回调返回给Js
//                function.onCallBack(str);
                ActivityUtils.startActivity(TestWebViewActivity.this, RegisterAddActivity.class);
            }
        });

//

    }

    @Override
    protected void initChildData() {

        WebSettings webSetting = webView.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setAppCachePath(this.getDir("appcache", 0).getPath());
        webSetting.setDatabasePath(this.getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(this.getDir("geolocation", 0)
                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        // webSetting.setPreFectch(true);
        long time = System.currentTimeMillis();
//        if (mIntentUrl == null) {
//            webView.loadUrl(mHomeUrl);
//        } else {
//            webView.loadUrl(mIntentUrl.toString());
//        }
        TbsLog.d("time-cost", "cost time: "
                + (System.currentTimeMillis() - time));

    }


    /**
     * 自定义的WebViewClient
     */
    class MyWebViewClient extends BridgeWebViewClient {

        public MyWebViewClient(BridgeWebView webView) {
            super(webView);
        }

    }

    @Override
    protected void setChildViewListener() {

    }
}
